package com.ymttest.business.service.shenqi;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.shop2cn.iapi.agentproducts.service.ProductDetailsCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.MchInfoVo;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.detailmchBean;
import com.ymatou.iapi.godmch.service.detailmchCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.database.model.YmtSubCatalogs;
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.database.sqlwapper.YmtSubCatalogsWapper;
import com.ymttest.utils.EnvSetup;

public class DGSQBuildProxyChainCallService {
	
	private static String autotest_goodsOwnerName = String.valueOf(EnvSetup
			.getData("autotest_goodsOwnerName"));
	
	public static AgentProductBean createGoodsOwnerProduct(Long goodsOwnerId,
			String currency, Boolean isForeignCurrency) throws Exception {

		String productId = "";

		if (!isForeignCurrency) {
			currency = "CNY";
			productId = DGSQBaseService.createAgentProduct(
					goodsOwnerId.intValue(), autotest_goodsOwnerName, true,
					currency);
		} else {

			currency = getMchInfoBySellerId((long) goodsOwnerId).getCurrency();
			productId = DGSQBaseService.createAgentProduct(
					goodsOwnerId.intValue(), autotest_goodsOwnerName, true,
					currency);
		}

		AgentProductBean agentProductBean = new AgentProductBean();

		agentProductBean.setProductId(productId);
		agentProductBean.setCatalogList(DGSQBaseService
				.getCatalogIdList(productId));
		agentProductBean.setSellerId(goodsOwnerId);
		agentProductBean.setCurrency(currency);
		agentProductBean.setSellerLoginId("autotest");
		agentProductBean.setGoodsOwnerId(goodsOwnerId);
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);

		return agentProductBean;
	}
	
	/**
	 * 创建供货商商品
	 * @param country
	 * @param currency
	 * @param isForeignCurrency
	 * @return
	 * @throws Exception
	 */
	public static AgentProductBean createSupplierProduct(Integer mchId, String country, 
			String currency, Boolean isForeignCurrency) throws Exception {

		String productId = "";
		
		Integer supplierId = DGSQBaseService.createApplySupplier(country, mchId);
		
		if (!isForeignCurrency) {
			currency = "CNY";
			productId = DGSQBaseService.createAgentProduct(
					supplierId.intValue(), "", false,
					currency);
		} else {

			currency = getMchInfoBySellerId((long) supplierId).getCurrency();
			productId = DGSQBaseService.createAgentProduct(
					supplierId.intValue(), "", false,
					currency);
		}

		AgentProductBean agentProductBean = new AgentProductBean();

		agentProductBean.setProductId(productId);
		agentProductBean.setCatalogList(DGSQBaseService
				.getCatalogIdList(productId));
		agentProductBean.setSellerId(supplierId);
		agentProductBean.setCurrency(currency);
		agentProductBean.setSellerLoginId("autotest");
		agentProductBean.setGoodsOwnerId((long)supplierId);
		
		DGSQBaseService.updateMchRelationToUnbind((long)mchId);

		return agentProductBean;
	}

	/**
	 * 创建代理链并将商品import给各个代理商
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param n
	 *            n个中间商
	 * @param needAgent
	 *            是否再添加一个代理
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createProxychain(int goodsOwnerId, int n,
			boolean needAgent) throws Exception {

		return createProxychain(goodsOwnerId, n, needAgent, false);
	}

	/**
	 * 创建代理链并将商品import给各个代理商(指定币种)
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param n
	 *            n个中间商
	 * @param needAgent
	 *            是否再添加一个代理
	 * @param currency
	 *            指定币种（mch_info表multiCurrency支持的币种）
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createProxychain(int goodsOwnerId, int n,
			boolean needAgent, String currency) throws Exception {

		AgentProductBean agentProductBean = null;
		int parentUserId = goodsOwnerId;
		String productId = "";
		String rootProductId = "";

		if (currency.equalsIgnoreCase("KRW"))
			currency = "USD";
		
		rootProductId = DGSQBaseService.createAgentProduct(goodsOwnerId,
				autotest_goodsOwnerName, true, currency);
		productId = rootProductId;
		
		if (n == 0) {
			
			agentProductBean = new AgentProductBean();
			
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper().selectDGUserInfo(
					parentUserId).get(0).getLoginId());
			agentProductBean.setProductId(rootProductId);
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
			agentProductBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
		}

		for (int i = 0; i < n; i++) {

			// 创建一个新的渠道商并绑定上级
			createmchBean createmchBean = DGSQBaseService
					.bindChannel(parentUserId);

			agentProductBean = DGSQBaseService
					.importAgentProduct(createmchBean.getMchId(), "",
							productId, parentUserId, true);
			parentUserId = createmchBean.getMchId();
			productId = agentProductBean.getProductId();
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(createmchBean.getMchId()).get(0)
					.getLoginId());
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setRootCatalogList(DGSQBaseService
					.getCatalogIdList(rootProductId));

			if (needAgent && i == n - 1) {

				createmchBean bean = DGSQBaseService.createAgent(
						createmchBean.getMchId(), "CHN");

				AgentProductBean agentProduct = DGSQBaseService
						.importAgentProduct(bean.getMchId(), "",
								agentProductBean.getProductId(),
								createmchBean.getMchId(), false);

				agentProduct.setSellerId(bean.getMchId());
				agentProduct.setSellerLoginId(new DGUserWapper()
						.selectDGUserInfo(bean.getMchId()).get(0).getLoginId());
				agentProduct.setCurrency(currency);
				agentProduct.setRootProductId(rootProductId);
				agentProduct.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
				agentProduct.setGoodsOwnerId((long)goodsOwnerId);

				return agentProduct;
			}
		}

		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)goodsOwnerId);
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);

		return agentProductBean;
	}
	
	/**
	 * 创建代理链并将商品import给各个代理商
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param n
	 *            n个中间商
	 * @param needAgent
	 *            是否再添加一个代理
	 * @param isForeignCurrency
	 *            是否使用外币
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createProxychain(int goodsOwnerId, int n,
			boolean needAgent, boolean isForeignCurrency) throws Exception {

		AgentProductBean agentProductBean = null;
		int parentUserId = goodsOwnerId;
		String productId = "";
		String currency = "";
		String rootProductId = "";
		
		List<Long> sellerIds = new ArrayList<>();

		if (!isForeignCurrency) {
			currency = "CNY";
			rootProductId = DGSQBaseService.createAgentProduct(goodsOwnerId,
					autotest_goodsOwnerName, true, currency);
		} else {
			
			currency = getMchInfoBySellerId((long)goodsOwnerId).getCurrency();
			
			if (currency.equalsIgnoreCase("KRW"))
				currency = "USD";
			
			rootProductId = DGSQBaseService.createAgentProduct(goodsOwnerId,
					autotest_goodsOwnerName, true, currency);
		}
		
		productId = rootProductId;
		sellerIds.add((long)parentUserId);
		
		if (n == 0) {
			
			agentProductBean = new AgentProductBean();

			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(parentUserId).get(0).getLoginId());
			agentProductBean.setProductId(rootProductId);
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setCatalogList(DGSQBaseService
					.getCatalogIdList(rootProductId));
			agentProductBean.setRootCatalogList(DGSQBaseService
					.getCatalogIdList(rootProductId));
		}

		for (int i = 0; i < n; i++) {

			// 创建一个新的渠道商并绑定上级
			createmchBean createmchBean = DGSQBaseService
					.bindChannel(parentUserId);

			agentProductBean = DGSQBaseService
					.importAgentProduct(createmchBean.getMchId(), "",
							productId, parentUserId, true);
			parentUserId = createmchBean.getMchId();
			productId = agentProductBean.getProductId();
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(createmchBean.getMchId()).get(0)
					.getLoginId());
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
			sellerIds.add((long)parentUserId);
			
			if (needAgent && i == n - 1) {

				createmchBean bean = DGSQBaseService.createAgent(
						createmchBean.getMchId(), "CHN");

				AgentProductBean agentProduct = DGSQBaseService
						.importAgentProduct(bean.getMchId(), "",
								agentProductBean.getProductId(),
								createmchBean.getMchId(), false);

				agentProduct.setSellerId(bean.getMchId());
				agentProduct.setSellerLoginId(new DGUserWapper()
						.selectDGUserInfo(bean.getMchId()).get(0).getLoginId());
				agentProduct.setCurrency(currency);
				agentProduct.setRootProductId(rootProductId);
				agentProduct.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
				agentProduct.setGoodsOwnerId((long)goodsOwnerId);
				
				sellerIds.add((long)bean.getMchId());
				
				agentProduct.setSellerIds(sellerIds);

				return agentProduct;
			}
		}

		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)goodsOwnerId);
		
		agentProductBean.setSellerIds(sellerIds);
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);

		return agentProductBean;
	}
	
	/**
	 * 创建继承代理链并将商品import给各个代理商
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param isForeignCurrency
	 *            是否使用外币
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createInheritedProxychain(int goodsOwnerId,
			boolean isForeignCurrency) throws Exception {

		String productId = "";
		String currency = "";

		if (!isForeignCurrency) {
			currency = "CNY";
		} else {
			currency = getMchInfoBySellerId((long)goodsOwnerId).getCurrency();
		}
		
		productId = DGSQBaseService.createAgentProduct(goodsOwnerId,
				autotest_goodsOwnerName, true, currency);
		
		Integer agentId = DGSQBaseService.createIndependenceAgent(
				goodsOwnerId, goodsOwnerId, "CHN", 2);
		
		AgentProductBean agentProductBean = new AgentProductBean();
		
		agentProductBean.setProductId(productId);
		agentProductBean.setCatalogList(DGSQBaseService.getCatalogIdList(productId));
		agentProductBean.setSellerId(agentId);
		agentProductBean.setSellerLoginId(new DGUserWapper()
				.selectDGUserInfo(agentId).get(0).getLoginId());
		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)goodsOwnerId);
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);

		return agentProductBean;
	}
	
	/**
	 * 创建继承代理链并将商品import给各个代理商
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param isForeignCurrency
	 *            是否使用外币
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createRetailShopProxychain(int goodsOwnerId,
			BigDecimal rebateRate, boolean isForeignCurrency) throws Exception {

		String productId = "";
		String currency = "";

		if (!isForeignCurrency) {
			currency = "CNY";
		} else {
			currency = getMchInfoBySellerId((long)goodsOwnerId).getCurrency();
		}
		
		productId = DGSQBaseService.createAgentProduct(goodsOwnerId,
				autotest_goodsOwnerName, true, currency);
		
		Integer retailShopId = DGSQBaseService.createIndependenceAgent(
				goodsOwnerId, goodsOwnerId, "CHN", 2);
		
		AgentProductBean agentProductBean = new AgentProductBean();
		
		agentProductBean.setProductId(productId);
		agentProductBean.setCatalogList(DGSQBaseService.getCatalogIdList(productId));
		agentProductBean.setSellerId(retailShopId);
		agentProductBean.setSellerLoginId(new DGUserWapper()
				.selectDGUserInfo(retailShopId).get(0).getLoginId());
		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)goodsOwnerId);
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);

		return agentProductBean;
	}
	
	/**
	 * 创建独立商家代理链并将商品import给各个代理商
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param isForeignCurrency
	 *            是否使用外币
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createIndependenceProxychain(int goodsOwnerId, int n, boolean retailShop,
			boolean isForeignCurrency) throws Exception {

		GodmchManageCallService.setPayModeBean(goodsOwnerId);
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);
		
		AgentProductBean agentProductBean = new AgentProductBean();
		int parentUserId = goodsOwnerId;
		String productId = "";
		String currency = "";
		String rootProductId = "";

		if (!isForeignCurrency) {
			currency = "CNY";
			rootProductId = DGSQBaseService.createAgentProduct(parentUserId, "",
					true, "CNY");
		} else {
			currency = getMchInfoBySellerId((long)goodsOwnerId).getCurrency();
			rootProductId = DGSQBaseService.createAgentProduct(parentUserId, "",
					true, currency);
		}
		
		productId = rootProductId;
		
		if (n == 0) {
			
			agentProductBean.setProductId(productId);
			agentProductBean.setCatalogList(DGSQBaseService.getCatalogIdList(productId));
			agentProductBean.setSellerId(goodsOwnerId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(goodsOwnerId).get(0).getLoginId());
			agentProductBean.setCurrency(currency);
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
			agentProductBean.setGoodsOwnerId((long)goodsOwnerId);
			
		} else {
			// 代理链上最多3个商家
			for (int i = 0; i < n; i++) {
				
				int createmchBeanUserId = 0;

				if (retailShop && i == n - 1) {

					int beanUserId = DGSQBaseService.createIndependenceAgent(parentUserId, goodsOwnerId, "USA", 2);
					
					agentProductBean.setProductId(productId);
					agentProductBean.setCatalogList(DGSQBaseService.getCatalogIdList(productId));
					agentProductBean.setSellerId(beanUserId);
					agentProductBean.setSellerLoginId(new DGUserWapper()
							.selectDGUserInfo(beanUserId).get(0).getLoginId());
					agentProductBean.setCurrency(currency);
					agentProductBean.setRootProductId(rootProductId);
					agentProductBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
					agentProductBean.setGoodsOwnerId((long)goodsOwnerId);

					return agentProductBean;
				} else {
					
					// 创建一个新的代理商并绑定上级
					createmchBeanUserId = DGSQBaseService
							.createIndependenceAgent(parentUserId, goodsOwnerId, "USA", 1);

					agentProductBean = DGSQBaseService
							.importAgentProduct(createmchBeanUserId, "",
									productId, parentUserId, true);
					
					parentUserId = createmchBeanUserId;
					productId = agentProductBean.getProductId();
					
					agentProductBean.setSellerId(parentUserId);
					agentProductBean.setSellerLoginId(new DGUserWapper()
							.selectDGUserInfo(createmchBeanUserId).get(0)
							.getLoginId());
					agentProductBean.setRootProductId(rootProductId);
					agentProductBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
					agentProductBean.setGoodsOwnerId((long)goodsOwnerId);
				}
			}
		}
		
		agentProductBean.setCurrency(currency);

		return agentProductBean;
	}
	
	/**
	 * 创建具有分销关系的代理链并将商品import给下级代理商
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param isForeignCurrency
	 *            是否使用外币
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createPromotersProxychain(
			int goodsOwnerId, int promoterNum, boolean isForeignCurrency) throws Exception {

		String productId = "";
		String rootProductId = "";
		String currency = "";
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);

		if (!isForeignCurrency) {
			currency = "CNY";
		} else {
			currency = getMchInfoBySellerId((long) goodsOwnerId).getCurrency();
		}

		rootProductId = DGSQBaseService.createAgentProduct(goodsOwnerId,
				autotest_goodsOwnerName, true, currency);

		Integer retailShopId = DGSQBaseService.createPromoters(goodsOwnerId, promoterNum);

		AgentProductBean agentProductBean = DGSQBaseService
				.importAgentProduct(retailShopId, "",
						rootProductId, goodsOwnerId, false);

		productId = agentProductBean.getProductId();
		
		agentProductBean.setSellerId(retailShopId);
		agentProductBean.setSellerLoginId(new DGUserWapper()
				.selectDGUserInfo(retailShopId).get(0)
				.getLoginId());
		agentProductBean.setRootProductId(rootProductId);
		agentProductBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(productId));
		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)retailShopId);
		agentProductBean.setProductId(productId);

		return agentProductBean;
	}

	/**
	 * 创建UK代理链并将商品import给各个代理商
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param n
	 *            n个中间商
	 * @param needAgent
	 *            是否再添加一个代理
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createUKProxychain(int goodsOwnerId, int n,
			boolean needAgent) throws Exception {

		return createUKProxychain(goodsOwnerId, n, needAgent, false);
	}

	/**
	 * 创建代理链并将商品import给各个代理商
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param n
	 *            n个中间商
	 * @param needAgent
	 *            是否再添加一个代理
	 * @param isForeignCurrency
	 *            是否使用外币支付
	 * 
	 * @return
	 * @throws Exception
	 */
	public static AgentProductBean createUKProxychain(int goodsOwnerId, int n,
			boolean needAgent, boolean isForeignCurrency) throws Exception {

		AgentProductBean agentProductBean = null;
		int parentUserId = goodsOwnerId;
		String productId = "";
		String currency = "";
		String rootProductId = "";
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);

		if (!isForeignCurrency) {
			currency = "CNY";
			rootProductId = DGSQBaseService.createAgentProduct(parentUserId, "",
					true, "CNY");
		} else {
			currency = getMchInfoBySellerId((long)goodsOwnerId).getCurrency();
			rootProductId = DGSQBaseService.createAgentProduct(parentUserId, "",
					true, currency);
		}
		
		productId = rootProductId;

		for (int i = 0; i < n; i++) {
			
			// 创建一个新的渠道商并绑定上级
			createmchBean createmchBean = DGSQBaseService.bindChannel(
					parentUserId, "GBR", null);

			agentProductBean = DGSQBaseService
					.importAgentProduct(createmchBean.getMchId(), "",
							productId, parentUserId, true);
			parentUserId = createmchBean.getMchId();
			productId = agentProductBean.getProductId();
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(createmchBean.getMchId()).get(0)
					.getLoginId());
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setRootCatalogList(DGSQBaseService
					.getCatalogIdList(rootProductId));

			if (needAgent && i == n - 1) {

				createmchBean bean = DGSQBaseService.createAgent(
						createmchBean.getMchId(), "CHN");

				AgentProductBean agentProduct = DGSQBaseService
						.importAgentProduct(bean.getMchId(), "",
								agentProductBean.getProductId(),
								createmchBean.getMchId(), false);

				agentProduct.setSellerId(bean.getMchId());
				agentProduct.setSellerLoginId(new DGUserWapper()
						.selectDGUserInfo(bean.getMchId()).get(0).getLoginId());
				agentProduct.setCurrency(currency);
				agentProduct.setRootProductId(rootProductId);
				agentProduct.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
				agentProduct.setGoodsOwnerId((long)goodsOwnerId);

				return agentProduct;
			}
		}

		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)goodsOwnerId);

		return agentProductBean;
	}

	/**
	 * 创建代理链并将商品import给各个代理商
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param n
	 *            n个中间商
	 * @param needAgent
	 *            是否再添加一个代理
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createJPProxychain(int goodsOwnerId, int n,
			boolean needAgent) throws Exception {

		return createJPProxychain(goodsOwnerId, n, needAgent, false);
	}

	/**
	 * 创建代理链并将商品import给各个代理商
	 * 
	 * @param goodsOwnerId 500058147
	 *            货头Id
	 * @param n
	 *            n个中间商
	 * @param needAgent
	 *            是否再添加一个代理
	 * @param isForeignCurrency
	 *            是否使用外币支付
	 * 
	 * @return
	 * @throws Exception
	 */
	public static AgentProductBean createJPProxychain(int goodsOwnerId, int n,
			boolean needAgent, boolean isForeignCurrency) throws Exception {

		AgentProductBean agentProductBean = null;
		int parentUserId = goodsOwnerId;
		String productId = "";
		String currency = "";
		String rootProductId = "";
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);

		if (!isForeignCurrency) {
			currency = "CNY";
			rootProductId = DGSQBaseService.createAgentProduct(parentUserId, "",
					true, "CNY");
		} else {
			currency = getMchInfoBySellerId((long)goodsOwnerId).getCurrency();
			rootProductId = DGSQBaseService.createAgentProduct(parentUserId, "",
					true, currency);
		}
		
		productId = rootProductId;
		
		if (n == 0) {
			
			agentProductBean = new AgentProductBean();
			
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper().selectDGUserInfo(
					parentUserId).get(0).getLoginId());
			agentProductBean.setProductId(rootProductId);
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
			agentProductBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
		}
		 
		for (int i = 0; i < n; i++) {

			// 创建一个新的渠道商并绑定上级
			createmchBean createmchBean = DGSQBaseService.bindChannel(
					parentUserId, "JPN", null);

			agentProductBean = DGSQBaseService
					.importAgentProduct(createmchBean.getMchId(), "",
							productId, parentUserId, true);
			parentUserId = createmchBean.getMchId();
			productId = agentProductBean.getProductId();
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(createmchBean.getMchId()).get(0)
					.getLoginId());
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));

			if (needAgent && i == n - 1) {

				createmchBean bean = DGSQBaseService.createAgent(
						createmchBean.getMchId(), "CHN");

				AgentProductBean agentProduct = DGSQBaseService
						.importAgentProduct(bean.getMchId(), "",
								agentProductBean.getProductId(),
								createmchBean.getMchId(), false);

				agentProduct.setSellerId(bean.getMchId());
				agentProduct.setSellerLoginId(new DGUserWapper()
						.selectDGUserInfo(bean.getMchId()).get(0).getLoginId());
				agentProduct.setCurrency(currency);
				agentProduct.setRootProductId(rootProductId);
				agentProduct.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
				agentProduct.setGoodsOwnerId((long)goodsOwnerId);

				return agentProduct;
			}
		}

		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)goodsOwnerId);

		return agentProductBean;
	}
	
	/**
	 * 创建代理链并将商品import给各个代理商
	 * 韩币不支持本国货币，所以还是使用CNY
	 * @param goodsOwnerId
	 *            货头Id
	 * @param n
	 *            n个中间商
	 * @param needAgent
	 *            是否再添加一个代理
	 * 
	 * @return
	 * @throws Exception
	 */
	public static AgentProductBean createKRProxychain(int goodsOwnerId, int n,
			boolean needAgent) throws Exception {

		AgentProductBean agentProductBean = null;
		int parentUserId = goodsOwnerId;
		String productId = "";
		String currency = "CNY";
		String rootProductId = "";
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);
		
		rootProductId = DGSQBaseService.createAgentProduct(parentUserId, "",
				true, currency);
		
		productId = rootProductId;

		for (int i = 0; i < n; i++) {

			// 创建一个新的渠道商并绑定上级
			createmchBean createmchBean = DGSQBaseService.bindChannel(
					parentUserId, "KOR", null);

			agentProductBean = DGSQBaseService
					.importAgentProduct(createmchBean.getMchId(), "",
							productId, parentUserId, true);
			parentUserId = createmchBean.getMchId();
			productId = agentProductBean.getProductId();
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(createmchBean.getMchId()).get(0)
					.getLoginId());
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));

			if (needAgent && i == n - 1) {

				createmchBean bean = DGSQBaseService.createAgent(
						createmchBean.getMchId(), "CHN");

				AgentProductBean agentProduct = DGSQBaseService
						.importAgentProduct(bean.getMchId(), "",
								agentProductBean.getProductId(),
								createmchBean.getMchId(), false);

				agentProduct.setSellerId(bean.getMchId());
				agentProduct.setSellerLoginId(new DGUserWapper()
						.selectDGUserInfo(bean.getMchId()).get(0).getLoginId());
				agentProduct.setCurrency(currency);
				agentProduct.setRootProductId(rootProductId);
				agentProduct.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
				agentProduct.setGoodsOwnerId((long)goodsOwnerId);

				return agentProduct;
			}
		}

		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)goodsOwnerId);

		return agentProductBean;
	}
	
	/**
	 * 创建新西兰代理链并将商品import给各个代理商
	 * 新西兰不支持本国货币
	 * @param goodsOwnerId
	 *            货头Id
	 * @param n
	 *            n个中间商
	 * @param needAgent
	 *            是否再添加一个代理
	 * 
	 * @return
	 * @throws Exception
	 */
	public static AgentProductBean createNZLProxychain(int goodsOwnerId, int n,
			boolean needAgent) throws Exception {

		AgentProductBean agentProductBean = null;
		int parentUserId = goodsOwnerId;
		String productId = "";
		String currency = "CNY";
		String rootProductId = "";
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);
		
		rootProductId = DGSQBaseService.createAgentProduct(parentUserId, "",
				true, currency);
		
		productId = rootProductId;
		
		if (n == 0) {
			
			agentProductBean = new AgentProductBean();
			
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(parentUserId).get(0).getLoginId());
			agentProductBean.setProductId(productId);
			agentProductBean.setRootProductId(productId);
			agentProductBean.setCatalogList(DGSQBaseService.getCatalogIdList(productId));
			agentProductBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(productId));
		}

		for (int i = 0; i < n; i++) {

			// 创建一个新的渠道商并绑定上级
			createmchBean createmchBean = DGSQBaseService.bindChannel(
					parentUserId, "NZL", null);

			agentProductBean = DGSQBaseService
					.importAgentProduct(createmchBean.getMchId(), "",
							productId, parentUserId, true);
			parentUserId = createmchBean.getMchId();
			productId = agentProductBean.getProductId();
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(createmchBean.getMchId()).get(0)
					.getLoginId());
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));

			if (needAgent && i == n - 1) {

				createmchBean bean = DGSQBaseService.createAgent(
						createmchBean.getMchId(), "CHN");

				AgentProductBean agentProduct = DGSQBaseService
						.importAgentProduct(bean.getMchId(), "",
								agentProductBean.getProductId(),
								createmchBean.getMchId(), false);

				agentProduct.setSellerId(bean.getMchId());
				agentProduct.setSellerLoginId(new DGUserWapper()
						.selectDGUserInfo(bean.getMchId()).get(0).getLoginId());
				agentProduct.setCurrency(currency);
				agentProduct.setRootProductId(rootProductId);
				agentProduct.setRootCatalogList(DGSQBaseService.getCatalogIdList(rootProductId));
				agentProduct.setGoodsOwnerId((long)goodsOwnerId);

				return agentProduct;
			}
		}

		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)goodsOwnerId);


		return agentProductBean;
	}
	
	/**
	 * 创建代理裂变代理链并将商品import给各个代理商
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param isForeignCurrency
	 *            是否使用外币
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createFissionProxychain(int goodsOwnerId,
			boolean isForeignCurrency) throws Exception {

		String productId = "";
		String currency = "";
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);

		if (!isForeignCurrency) {
			currency = "CNY";
		} else {
			currency = getMchInfoBySellerId((long)goodsOwnerId).getCurrency();
		}
		
		productId = DGSQBaseService.createAgentProduct(goodsOwnerId,
				autotest_goodsOwnerName, true, currency);
		
		Integer agentId = DGSQBaseService.createIndependenceAgent(
				goodsOwnerId, goodsOwnerId, "CHN", 2);
		
		AgentProductBean agentProductBean = new AgentProductBean();
		
		agentProductBean.setProductId(productId);
		agentProductBean.setCatalogList(DGSQBaseService.getCatalogIdList(productId));
		agentProductBean.setSellerId(agentId);
		agentProductBean.setSellerLoginId(new DGUserWapper()
				.selectDGUserInfo(agentId).get(0).getLoginId());
		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)goodsOwnerId);

		return agentProductBean;
	}
	
	/**
	 * 创建渠道商
	 * @param parentUserId
	 * @param productId
	 * @param country
	 * @return
	 * @throws Exception
	 */
	public static AgentProductBean createChannel(long parentUserId,
			String productId, String country) throws Exception {

		AgentProductBean agentProductBean = null;

		// 创建一个新的渠道商并绑定上级
		createmchBean createmchBean = DGSQBaseService.bindChannel((int)parentUserId,
				country, null);
		String currency = getMchInfoBySellerId((long) createmchBean.getMchId())
				.getCurrency();

		agentProductBean = DGSQBaseService.importAgentProduct(
				createmchBean.getMchId(), "", productId, (int)parentUserId, true);

		parentUserId = createmchBean.getMchId();
		productId = agentProductBean.getProductId();
		agentProductBean.setSellerId(parentUserId);
		agentProductBean.setSellerLoginId(new DGUserWapper()
				.selectDGUserInfo(createmchBean.getMchId()).get(0)
				.getLoginId());

		agentProductBean.setCurrency(currency);

		return agentProductBean;
	}
	
	/**
	 * 创建渠道商
	 * 
	 * @param parentUserId
	 * @param productId
	 * @param country
	 * @return
	 * @throws Exception
	 */
	public static AgentProductBean createChannel(long parentUserId,
			String productId, String country,
			AgentProductBean goodsOwnerProductBean) throws Exception {

		AgentProductBean agentProductBean = null;

		// 创建一个新的渠道商并绑定上级
		createmchBean createmchBean = DGSQBaseService.bindChannel(
				(int) parentUserId, country, null);
		String currency = getMchInfoBySellerId((long) createmchBean.getMchId())
				.getCurrency();

		// productId = DGSQBaseService.createAgentProduct(parentUserId,
		// autotest_goodsOwnerName, true, currency);

		agentProductBean = DGSQBaseService.importAgentProduct(
				createmchBean.getMchId(), "", productId, (int) parentUserId,
				true);

		parentUserId = createmchBean.getMchId();
		productId = agentProductBean.getProductId();
		agentProductBean.setSellerId(parentUserId);
		agentProductBean
				.setSellerLoginId(new DGUserWapper()
						.selectDGUserInfo(createmchBean.getMchId()).get(0)
						.getLoginId());

		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId(goodsOwnerProductBean.getSellerId());

		return agentProductBean;
	}
	
	/**
	 * 创建渠道商
	 * @param parentUserId
	 * @param productId
	 * @param country
	 * @return
	 * @throws Exception
	 */
	public static AgentProductBean createChannel(long parentUserId,
			String productId, String country, Long sellerId) throws Exception {

		return createChannel(parentUserId, productId, country, sellerId, null);
	}
	
	/**
	 * 创建渠道商
	 * 
	 * @param parentUserId
	 * @param productId
	 * @param country
	 * @return
	 * @throws Exception
	 */
	public static AgentProductBean createChannel(long parentUserId,
			String productId, String country, Long sellerId,
			AgentProductBean goodsOwnerProductBean) throws Exception {

		AgentProductBean agentProductBean = null;

		// 创建一个新的渠道商并绑定上级
		DGSQBaseService.bindChannel((int) parentUserId, country,
				sellerId.intValue());

		String currency = getMchInfoBySellerId(sellerId).getCurrency();

		agentProductBean = DGSQBaseService.importAgentProduct(
				sellerId.intValue(), "", productId, (int) parentUserId, true);

		// parentUserId = createmchBean.getMchId();
		productId = agentProductBean.getProductId();
		agentProductBean.setSellerId(sellerId);
		agentProductBean.setSellerLoginId(new DGUserWapper()
				.selectDGUserInfo(sellerId.intValue()).get(0).getLoginId());
		agentProductBean.setGoodsOwnerId(null == goodsOwnerProductBean ? null
				: goodsOwnerProductBean.getSellerId());

		agentProductBean.setCurrency(currency);

		return agentProductBean;
	}
	
	public static AgentProductBean createIndependenceChannel(long parentUserId,
			long goodsOwnerId, String productId, String country) throws Exception {

		GodmchManageCallService.setPayModeBean(goodsOwnerId);

		AgentProductBean agentProductBean = new AgentProductBean();

		int createmchBeanUserId = 0;

		// 创建一个新的代理商并绑定上级
		createmchBeanUserId = DGSQBaseService.createIndependenceAgent(
				parentUserId, goodsOwnerId, country, 1);

		agentProductBean = DGSQBaseService.importAgentProduct(
				createmchBeanUserId, "", productId, (int)parentUserId, true);

		agentProductBean.setSellerId(createmchBeanUserId);
		agentProductBean.setSellerLoginId(new DGUserWapper()
				.selectDGUserInfo(createmchBeanUserId).get(0).getLoginId());
		agentProductBean.setGoodsOwnerId(goodsOwnerId);
//		agentProductBean.setRootProductId(rootProductId);
//		agentProductBean.setRootCatalogList(DGSQBaseService
//				.getCatalogIdList(rootProductId));
//
//		agentProductBean.setCurrency(currency);

		return agentProductBean;
	}
	
	/**
	 * 创建代理商
	 * @param sellerId
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	public static AgentProductBean createAgent(long sellerId, String productId, String country) throws Exception {

		createmchBean bean = DGSQBaseService.createAgent(
				(int)sellerId, country);
		String currency = getMchInfoBySellerId((long) bean.getMchId())
				.getCurrency();

		AgentProductBean agentProduct = DGSQBaseService.importAgentProduct(
				bean.getMchId(), "", productId, (int) sellerId, false);

		agentProduct.setSellerId(bean.getMchId());
		agentProduct.setSellerLoginId(new DGUserWapper()
				.selectDGUserInfo(bean.getMchId()).get(0).getLoginId());
		agentProduct.setCurrency(currency);

		return agentProduct;
	}

	/**
	 * 根據mchId 获取渠道商信息
	 * 
	 * @param mchId
	 * @return
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static MchInfoVo getMchInfoBySellerId(Long mchId) throws ClientProtocolException, IOException {

		detailmchBean detailmchbean = new detailmchBean();
		detailmchCall detailmchcall = new detailmchCall();
		
		detailmchbean.setMchId(mchId.intValue()); 

		detailmchcall.setData(detailmchbean);
		detailmchcall.callService();
		
		return detailmchcall.getResult();
	}

	/**
	 * 客服绑定
	 * 
	 * @param mchId
	 *            商户Id
	 * @param template
	 *            客服权限：1（基本权限），4（所有权限）
	 * @return
	 * @throws Exception
	 */
	public static int bindClientService(long mchId, int template) {

		int csId = 0;

		try {
			UserCreateBean usercreateBean = DGUserCallService
					.initCsUsercreateBean();
			csId = DGUserCallService.UserCreateCall(usercreateBean);
			GodmchManageCallService.CreateCSCall((int) mchId, csId, template);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Logger.comment("创建渠道商客服:" + csId);

		return csId;
	}
	
	public static ProductDetailsDto getProductDetails(Long userId,
			String productId) throws ClientProtocolException, IOException {

		ProductDetailsBean productDetailsBean = new ProductDetailsBean();
		ProductDetailsCall productdetailsCall = new ProductDetailsCall();

		productDetailsBean.setProductId(productId);
		productDetailsBean.setUserId(userId.intValue());

		productdetailsCall.setData(productDetailsBean);
		productdetailsCall.callService();

		String product = productdetailsCall.getJsonObject().get("data")
				.toString();

		ProductDetailsDto productDetailsDto = (ProductDetailsDto) FormatUtil
				.GSONToObject(product, ProductDetailsDto.class);

		return productDetailsDto;
	}
	
	/**
	 * 创建盛付通代理链并将商品import给各个代理商
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param n
	 *            n个中间商
	 *            是否全部开通盛付通
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createShengPayProxychain(int goodsOwnerId,
			int n) throws Exception {

		AgentProductBean agentProductBean = null;
		int parentUserId = goodsOwnerId;
		String productId = "";
		
		String currency = "CNY";
		productId = DGSQBaseService.createAgentProduct(goodsOwnerId,
				autotest_goodsOwnerName, true, currency);
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);
			
		if (n == 0) {
			agentProductBean = new AgentProductBean();
			
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper().selectDGUserInfo(
					parentUserId).get(0).getLoginId());
			agentProductBean.setProductId(productId);
			agentProductBean.setRootProductId(productId);
			agentProductBean.setCatalogList(DGSQBaseService.getCatalogIdList(productId));
			agentProductBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(productId));
		}

		for (int i = 0; i < n; i++) {

			// 创建一个新的渠道商并绑定上级
			createmchBean createmchBean = DGSQBaseService
					.bindShengPayChannel(parentUserId, null);

			agentProductBean = DGSQBaseService
					.importAgentProduct(createmchBean.getMchId(), "",
							productId, parentUserId, true);
			
			parentUserId = createmchBean.getMchId();
			productId = agentProductBean.getProductId();
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(createmchBean.getMchId()).get(0)
					.getLoginId());
		}

		agentProductBean.setCurrency(currency);

		return agentProductBean;
	}
	
	/**
	 * 创建白名单商户代理链并将商品import给各个代理商
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param isForeignCurrency
	 *            是否使用外币
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createWhiteMerchantProxychain(
			int goodsOwnerId, boolean isForeignCurrency)
			throws Exception {
		
		AgentProductBean agentProductBean = new AgentProductBean();
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);

		String productId;
		String currency;
		
		if (!isForeignCurrency) {
			currency = "CNY";
			productId = DGSQBaseService.createAgentProduct(goodsOwnerId,
					autotest_goodsOwnerName, true, currency);
		} else {
			
			currency = getMchInfoBySellerId((long)goodsOwnerId).getCurrency();
			productId = DGSQBaseService.createAgentProduct(goodsOwnerId,
					autotest_goodsOwnerName, true, currency);
		}

		agentProductBean.setProductId(productId);
		agentProductBean.setSellerId(goodsOwnerId);
		agentProductBean.setSellerLoginId(new DGUserWapper()
				.selectDGUserInfo(goodsOwnerId).get(0).getLoginId());
		agentProductBean.setCatalogList(DGSQBaseService
				.getCatalogIdList(productId));
		agentProductBean.setCurrency(currency);

		return agentProductBean;
	}
	
	/**
	 * 创建盛付通白名单代理链并将商品import给各个代理商
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param n
	 *            n个中间商
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createShengPayWhiteMerchantProxychain(int goodsOwnerId,
			int n) throws Exception {

		AgentProductBean agentProductBean = null;
		int parentUserId = goodsOwnerId;
		String productId = "";
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);
		
		String currency = "CNY";
		productId = DGSQBaseService.createAgentProduct(goodsOwnerId,
				autotest_goodsOwnerName, true, currency);

		for (int i = 0; i < n; i++) {

			// 创建一个新的渠道商并绑定上级
			createmchBean createmchBean = DGSQBaseService
					.bindShengPayChannel(parentUserId, null);

			agentProductBean = DGSQBaseService
					.importAgentProduct(createmchBean.getMchId(), "",
							productId, parentUserId, true);
			
			if (i == 0) {
				
				// 设置下级费率百分比
				GodmchManageCallService.setAgentRate(goodsOwnerId,
						403855, new BigDecimal("15"));
			}
			
			parentUserId = createmchBean.getMchId();
			productId = agentProductBean.getProductId();
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(createmchBean.getMchId()).get(0)
					.getLoginId());
		}

		agentProductBean.setCurrency(currency);

		return agentProductBean;
	}
	
	public static void modifyGoodsOwnerProdStock(int goodsOwnerId,
			AgentProductBean agentProductBean, int virtualStock, int totalStock)
			throws InterruptedException, IOException {

		DGSQBaseService.modifyAgentProductBean(goodsOwnerId, "goodsOwnerName",
				true, agentProductBean.getRootProductId(),
				agentProductBean.getCurrency(), virtualStock, totalStock);
	}
	
	public static AgentProductBean importAgentProduct(long mchId,
			AgentProductBean parentProductBean) throws InterruptedException,
			IOException {

		AgentProductBean productBean = new AgentProductBean();

		productBean = DGSQBaseService.importAgentProduct((int) mchId,
				productBean.getSellerLoginId(), parentProductBean.getProductId(),
				(int) parentProductBean.getSellerId(), true);
		
		productBean.setGoodsOwnerId(parentProductBean.getGoodsOwnerId());
		productBean.setSellerId(mchId);
		productBean.setSellerLoginId("sellerLoginId");
		
		return productBean;
	}
	
	/**
	 * 创建组合商品代理链
	 * @param goodsOwnerId
	 * @param isForeignCurrency
	 * @param isMultiCode
	 * @return
	 * @throws Exception
	 */
	public static AgentProductBean createComposeProductProxychain(
			int goodsOwnerId, Boolean isForeignCurrency, Boolean isMultiCode) throws Exception {

		AgentProductBean agentProductBean = null;
		int parentUserId = goodsOwnerId;
		String currency = "";
		String rootProductId = "";
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);
		
		if (!isForeignCurrency) {
			currency = "CNY";
			rootProductId = DGSQBaseService.createComposeAgentProduct(
					goodsOwnerId, currency, isMultiCode);
		} else {

			currency = getMchInfoBySellerId((long) goodsOwnerId).getCurrency();

			if (currency.equalsIgnoreCase("KRW"))
				currency = "USD";

			rootProductId = DGSQBaseService.createComposeAgentProduct(
					goodsOwnerId, currency, isMultiCode);
		}

		agentProductBean = new AgentProductBean();
		
		agentProductBean.setSellerId(parentUserId);
		agentProductBean.setSellerLoginId(new DGUserWapper().selectDGUserInfo(
				parentUserId).get(0).getLoginId());
		agentProductBean.setProductId(rootProductId);
		agentProductBean.setRootProductId(rootProductId);
		
		List<String> catalogIds = DGSQBaseService.getCatalogIdList(rootProductId);
		
		agentProductBean.setCatalogList(catalogIds);
		agentProductBean.setRootCatalogList(catalogIds);
		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)goodsOwnerId);
		
		// 如果商家是中免大会员，则将子商品sku修改成中免指定sku
		if (goodsOwnerId == 502060981) {

			YmtSubCatalogsWapper ymtSubCatalogsWapper = new YmtSubCatalogsWapper();

			for (String catalogId : catalogIds) {

				List<YmtSubCatalogs> ymtSubCatalogsList = ymtSubCatalogsWapper
						.selectListByCatalogId(catalogId);

				for (YmtSubCatalogs ymtSubCatalogs : ymtSubCatalogsList) {

					// 积分使用sku：C052900，cdf优惠券使用：C042283

					ymtSubCatalogsWapper.updateSkuToCDFMember(ymtSubCatalogs
							.getSubCatalogId().toString(), "C042283");
					ymtSubCatalogsWapper.updateBarCodeToCDFMember(
							ymtSubCatalogs.getSubCatalogId().toString(),
							"C042283");
				}
			}
		} else if (goodsOwnerId == 502665591) {
			
			YmtSubCatalogsWapper ymtSubCatalogsWapper = new YmtSubCatalogsWapper();

			for (String catalogId : catalogIds) {

				List<YmtSubCatalogs> ymtSubCatalogsList = ymtSubCatalogsWapper
						.selectListByCatalogId(catalogId);

				for (YmtSubCatalogs ymtSubCatalogs : ymtSubCatalogsList) {

					// 积分使用C040917，cdf优惠券使用：C042283

					ymtSubCatalogsWapper.updateSkuToCDFMember(ymtSubCatalogs
							.getSubCatalogId().toString(), "C040917");
					ymtSubCatalogsWapper.updateBarCodeToCDFMember(
							ymtSubCatalogs.getSubCatalogId().toString(),
							"C040917");
				}
			}
		}
	    
		return agentProductBean;
	}
	
	public static AgentProductBean createFundNotCollectProxychain(int goodsOwnerId,
			boolean isForeignCurrency) throws Exception {

		String productId = "";
		String currency = "";
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);

		if (!isForeignCurrency) {
			currency = "CNY";
		} else {
			currency = getMchInfoBySellerId((long)goodsOwnerId).getCurrency();
		}
		
		productId = DGSQBaseService.createAgentProduct(goodsOwnerId,
				autotest_goodsOwnerName, true, currency);
		
		Integer agentId = DGSQBaseService.createIndependenceAgent(
				goodsOwnerId, goodsOwnerId, "CHN", 2);
		
		AgentProductBean agentProductBean = new AgentProductBean();
		
		agentProductBean.setProductId(productId);
		agentProductBean.setCatalogList(DGSQBaseService.getCatalogIdList(productId));
		agentProductBean.setSellerId(agentId);
		agentProductBean.setSellerLoginId(new DGUserWapper()
				.selectDGUserInfo(agentId).get(0).getLoginId());
		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)goodsOwnerId);

		return agentProductBean;
	}
	
	/**
	 * 创建一品多码商品代理链
	 * @param goodsOwnerId
	 * @param isForeignCurrency
	 * @return
	 * @throws Exception
	 */
	public static AgentProductBean createMultiCodeProxychain(int goodsOwnerId,
			boolean isForeignCurrency) throws Exception {

		String productId = "";
		String currency = "";
		
		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);

		if (!isForeignCurrency) {
			currency = "CNY";
		} else {
			currency = getMchInfoBySellerId((long)goodsOwnerId).getCurrency();
		}
		
		productId = DGSQBaseService.createMultiCodeAgentProduct(goodsOwnerId,
				autotest_goodsOwnerName, true, currency);
		
		AgentProductBean agentProductBean = new AgentProductBean();
		
		agentProductBean.setProductId(productId);
		agentProductBean.setCatalogList(DGSQBaseService.getCatalogIdList(productId));
		agentProductBean.setSellerId(goodsOwnerId);
		agentProductBean.setSellerLoginId(new DGUserWapper()
				.selectDGUserInfo(goodsOwnerId).get(0).getLoginId());
		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)goodsOwnerId);
		agentProductBean.setRootProductId(productId);
		agentProductBean.setRootCatalogList(agentProductBean.getCatalogList());

		return agentProductBean;
	}

	/**
	 * 创建一品多码商品代理链
	 * @param goodsOwnerId
	 * @param isForeignCurrency
	 * @return
	 * @throws Exception
	 */
	public static AgentProductBean createMultiCodeProxychainForPreSale(int goodsOwnerId,
															 boolean isForeignCurrency) throws Exception {

		String productId = "";
		String currency = "";

		DGSQBaseService.updateMchRelationToUnbind((long)goodsOwnerId);

		if (!isForeignCurrency) {
			currency = "CNY";
		} else {
			currency = getMchInfoBySellerId((long)goodsOwnerId).getCurrency();
		}

		productId = DGSQBaseService.createMultiCodeAgentProductForPreSale(goodsOwnerId,
				autotest_goodsOwnerName, true, currency, 5);

		AgentProductBean agentProductBean = new AgentProductBean();

		agentProductBean.setProductId(productId);
		agentProductBean.setCatalogList(DGSQBaseService.getCatalogIdList(productId));
		agentProductBean.setSellerId(goodsOwnerId);
		agentProductBean.setSellerLoginId(new DGUserWapper()
				.selectDGUserInfo(goodsOwnerId).get(0).getLoginId());
		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long) goodsOwnerId);
		agentProductBean.setRootProductId(productId);
		agentProductBean.setRootCatalogList(agentProductBean.getCatalogList());

		return agentProductBean;
	}
	
	/**
	 * 创建代理链并将商品import给各个代理商(指定币种)
	 * 
	 * @param goodsOwnerId
	 *            货头Id
	 * @param n
	 *            n个中间商
	 * @param needAgent
	 *            是否再添加一个代理
	 * @param currency
	 *            指定币种（mch_info表multiCurrency支持的币种）
	 * @return AgentProductBean
	 * @throws Exception
	 */
	public static AgentProductBean createPlatformProxychain(int goodsOwnerId, int n,
			boolean needAgent, String currency) throws Exception {
		
		AgentProductBean agentProductBean = null;
		int parentUserId = goodsOwnerId;
		String productId = "";
		String rootProductId = "";

		if (currency.equalsIgnoreCase("KRW"))
			currency = "USD";
		
		rootProductId = DGSQBaseService.createAgentProduct(goodsOwnerId,
				autotest_goodsOwnerName, true, currency);
		productId = rootProductId;
		
		int mchId = 0;
		
		if (n == 0) {
			
			agentProductBean = new AgentProductBean();

			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(parentUserId).get(0).getLoginId());
			agentProductBean.setProductId(rootProductId);
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setCatalogList(DGSQBaseService
					.getCatalogIdList(rootProductId));
			agentProductBean.setRootCatalogList(DGSQBaseService
					.getCatalogIdList(rootProductId));
		}
		
		for (int i = 0; i < n; i++) {
			
			if (i == 0)
				mchId = 500002490;
			else
				mchId = DGSQBaseService.bindChannel(parentUserId).getMchId();

			agentProductBean = DGSQBaseService
					.importAgentProduct(mchId, "",
							productId, parentUserId, true);
			
			parentUserId = mchId;
			productId = agentProductBean.getProductId();
			
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(mchId).get(0)
					.getLoginId());
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setRootCatalogList(DGSQBaseService
					.getCatalogIdList(rootProductId));

			if (needAgent && i == n - 1) {

				createmchBean bean = DGSQBaseService.createAgent(mchId, "CHN");

				AgentProductBean agentProduct = DGSQBaseService
						.importAgentProduct(bean.getMchId(), "",
								agentProductBean.getProductId(), mchId, false);

				agentProduct.setSellerId(bean.getMchId());
				agentProduct.setSellerLoginId(new DGUserWapper()
						.selectDGUserInfo(bean.getMchId()).get(0).getLoginId());
				agentProduct.setCurrency(currency);
				agentProduct.setRootProductId(rootProductId);
				agentProduct.setRootCatalogList(DGSQBaseService
						.getCatalogIdList(rootProductId));
				agentProduct.setGoodsOwnerId((long) goodsOwnerId);

				return agentProduct;
			}
		}

		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long) goodsOwnerId);

		return agentProductBean;
	}
	
	public static AgentProductBean createPlatformProxychain(int goodsOwnerId, int firstChannelId, int n,
			boolean needAgent, String currency) throws Exception {
		
		AgentProductBean agentProductBean = null;
		int parentUserId = goodsOwnerId;
		String productId = "";
		String rootProductId = "";

		if (currency.equalsIgnoreCase("KRW"))
			currency = "USD";
		
		rootProductId = DGSQBaseService.createAgentProduct(goodsOwnerId,
				autotest_goodsOwnerName, true, currency);
		productId = rootProductId;
		
		int mchId = 0;
		
		for (int i = 0; i < n; i++) {
			
			if (i == 0)
				mchId = firstChannelId;
			else
				mchId = DGSQBaseService.bindChannel(parentUserId).getMchId();

			agentProductBean = DGSQBaseService
					.importAgentProduct(mchId, "",
							productId, parentUserId, true);
			
			parentUserId = mchId;
			productId = agentProductBean.getProductId();
			
			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(mchId).get(0)
					.getLoginId());
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setRootCatalogList(DGSQBaseService
					.getCatalogIdList(rootProductId));

			if (needAgent && i == n - 1) {

				createmchBean bean = DGSQBaseService.createAgent(mchId, "CHN");

				AgentProductBean agentProduct = DGSQBaseService
						.importAgentProduct(bean.getMchId(), "",
								agentProductBean.getProductId(), mchId, false);

				agentProduct.setSellerId(bean.getMchId());
				agentProduct.setSellerLoginId(new DGUserWapper()
						.selectDGUserInfo(bean.getMchId()).get(0).getLoginId());
				agentProduct.setCurrency(currency);
				agentProduct.setRootProductId(rootProductId);
				agentProduct.setRootCatalogList(DGSQBaseService
						.getCatalogIdList(rootProductId));
				agentProduct.setGoodsOwnerId((long) goodsOwnerId);

				return agentProduct;
			}
		}

		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long) goodsOwnerId);

		return agentProductBean;
	}
	
	public static AgentProductBean createPlatformIndependenceProxychain(
			int goodsOwnerId, int firstChannelId, int n, String currency)
			throws Exception {

		AgentProductBean agentProductBean = null;
		int parentUserId = goodsOwnerId;
		String productId = "";
		String rootProductId = "";

		if (currency.equalsIgnoreCase("KRW"))
			currency = "USD";

		rootProductId = DGSQBaseService.createAgentProduct(goodsOwnerId,
				autotest_goodsOwnerName, true, currency);
		productId = rootProductId;

		int mchId = 0;

		for (int i = 0; i < n; i++) {

			if (i == 0)
				mchId = firstChannelId;
			else
				mchId = DGSQBaseService.createIndependenceAgent(parentUserId,
						firstChannelId, "USA", 1);

			agentProductBean = DGSQBaseService.importAgentProduct(mchId, "",
					productId, parentUserId, true);

			parentUserId = mchId;
			productId = agentProductBean.getProductId();

			agentProductBean.setSellerId(parentUserId);
			agentProductBean.setSellerLoginId(new DGUserWapper()
					.selectDGUserInfo(mchId).get(0).getLoginId());
			agentProductBean.setRootProductId(rootProductId);
			agentProductBean.setRootCatalogList(DGSQBaseService
					.getCatalogIdList(rootProductId));
		}

		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long) goodsOwnerId);

		return agentProductBean;
	}
	
	public static AgentProductBean createPlatformSupplierPromotersProxychain(
			int goodsOwnerId, boolean isForeignCurrency) throws Exception {

		String productId = "";
		String rootProductId = "";
		String currency = "";
		
//		DGSQBaseService.updateMchRelationToUnbind((long) goodsOwnerId);

		if (!isForeignCurrency) {
			currency = "CNY";
		} else {
			currency = getMchInfoBySellerId((long) goodsOwnerId).getCurrency();
		}

		rootProductId = DGSQBaseService.createAgentProduct(goodsOwnerId,
				autotest_goodsOwnerName, true, currency);

		Integer retailShopId = 510011911;

		AgentProductBean agentProductBean = DGSQBaseService
				.importAgentProduct(retailShopId, "",
						rootProductId, goodsOwnerId, false);

		productId = agentProductBean.getProductId();
		
		agentProductBean.setSellerId(retailShopId);
		agentProductBean.setSellerLoginId(new DGUserWapper()
				.selectDGUserInfo(retailShopId).get(0)
				.getLoginId());
		agentProductBean.setRootProductId(rootProductId);
		agentProductBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(productId));
		agentProductBean.setCurrency(currency);
		agentProductBean.setGoodsOwnerId((long)retailShopId);
		agentProductBean.setProductId(productId);

		return agentProductBean;
	}
	
	public static void main(String[] args) throws Exception {

		// TradingForDGSQCallService.getProxyChainInfo("c2285350");

		// TradingForDGSQCallService.getAllParents(500004281);

//		TradingForDGSQCallService.createKRProxychain(500072235, 1, false);
		
//		TradingForDGSQCallService.createNZLProxychain(500173397, 1, false);
		
//		TradingForDGSQCallService.createInheritedProxychain((int) autotest_goodsOwnerId, false);
//		Logger.debug(TradingForDGSQCallService.isInheritedAgent(500221994));

//		DGSQBaseService.createIndependenceMch("USA");
//		AgentProductBean agentProductBean = DGSQBuildProxyChainCallService.createSupplierProduct(mchId, "USA",
//				"CNY", false);
//		
//		agentProductBean = DGSQBaseService.importAgentProduct(mchId,
//				agentProductBean.getSellerLoginId(),
//				agentProductBean.getProductId(),
//				(int) agentProductBean.getSellerId(), true);
//		agentProductBean = DGSQBuildProxyChainCallService
//				.createIndependenceChannel(mchId, mchId,
//						agentProductBean.getProductId(), "USA");
//
//		AgentProductBean agentProductBean2 = DGSQBuildProxyChainCallService
//				.createSupplierProduct(mchId, "USA", "CNY", false);
//		
//		agentProductBean2 = DGSQBaseService.importAgentProduct(mchId,
//				agentProductBean2.getSellerLoginId(),
//				agentProductBean2.getProductId(),
//				(int) agentProductBean2.getSellerId(), true);
//		
//		DGSQBaseService.importAgentProduct((int)agentProductBean.getSellerId(),
//				agentProductBean.getSellerLoginId(),
//				agentProductBean2.getProductId(),
//				(int)agentProductBean2.getSellerId(), true);
		
//		DGSQBuildProxyChainCallService.createIndependenceChannel(parentUserId, goodsOwnerId, productId, country);
		
//		DGSQBuildProxyChainCallService.createIndependenceProxychain(mchId, 4, true, false);
//		Integer agentId = DGSQBaseService.createIndependenceAgent(mchId, mchId, "CHN", 1);
//		DGSQBaseService.createRetailShopAgent(agentId, BigDecimal.TEN);
//		Integer agentId2 = DGSQBaseService.createIndependenceAgent(agentId, mchId, "CHN", 1);
//		Integer retailShopId = DGSQBaseService.createIndependenceAgent(agentId2, mchId, "CHN", 2);
//		
//		System.out.println("mchId=" + mchId + ",agentId=" + agentId + ",retailShopId=" + retailShopId);
		
//		Integer mchId = DGSQBaseService.createIndependenceMch("USA");
//		TradingForDGSQCallService.createIndependenceProxychain(mchId, 2, false, false);
		
//		TradingForDGSQCallService.getProductDetails(500002490L, "p2717072");

//		Logger.debug(TradingForDGSQCallService.getMchInfoBySellerId(500325125L));
		
//		TradingForDGSQCallService.createWhiteMerchantProxychain(500400836, false);
		
//		TradingForDGSQCallService.setRiskAmount(500583265, new BigDecimal("10000"), new BigDecimal("20000"));
		
//		System.out.println(TradingForDGSQCallService.getAgentRate(500401087, 500403652));
		
//		Integer mchId = DGSQBaseService.createIndependenceMch("USA");
//		
//		// 创建第一个供货商商品
//		AgentProductBean supplierProductBean_1 = DGSQBuildProxyChainCallService
//				.createSupplierProduct(mchId, "USA", "CNY", false);
//
//		AgentProductBean agentProductBean_1 = DGSQBuildProxyChainCallService
//				.importAgentProduct(mchId, supplierProductBean_1);
//
//		agentProductBean_1 = DGSQBuildProxyChainCallService
//				.createIndependenceChannel(mchId, mchId,
//						agentProductBean_1.getProductId(), "HKG");
//		agentProductBean_1.setGoodsOwnerId(supplierProductBean_1
//				.getGoodsOwnerId());
		
		DGSQBuildProxyChainCallService.createPlatformProxychain(502666058,
				500400836, 1, false, "CNY");
//		
//		TradingForDGSQCallService.placeOrder(agentProductBean_1, agentProductBean_1.getSellerId());

//		AgentProductBean japanProductBean = TradingForDGSQCallService.createSupplierProduct("JPN", "CNY", false);
//
//		TradingForDGSQCallService.createChannel(japanProductBean.getSellerId(),
//				japanProductBean.getProductId(), productBean.getCurrency(), productBean.getSellerId());

//		Logger.debug(TradingForDGSQCallService.getAmountByCurrency(
//				new BigDecimal("972.00"), "CNY", "JPY"));

//		 AgentProductBean productBean = new AgentProductBean();
//		
//		 productBean.setSellerId(500004881);
//		 productBean.setSellerLoginId("autotest500004881");
//		 productBean.setProductId("p1466630");
//		 productBean.setCatalogList(Lists.newArrayList("c2403528",
//		 "c2403527"));
		//
		// PlaceOrderReq req =
		// TradingForDGSQCallService.buildDGSQStandPlaceOrderReq(0,
		// BigDecimal.ZERO, 20238699, productBean, autotest_goodsOwnerId);
		//
		// TradingForDGSQCallService.callPlaceOrderService(req);
	}
}
